﻿using System;
using System.IO;
using DuplicateFileFinder.Infrastructure;
using DuplicateFileFinder.Model;

namespace DuplicateFileFinder.Business
{
    public class SearchProcess : ISearchProcess
    {

        #region ISearchProcess Member

        public void StartSearch(object searchPath)
        {
            SearchResultQueue.Instance.Clear();
            Search(searchPath);
        }

        #endregion

        private void Search(object searchPathInfo)
        {
            var currPath = searchPathInfo as String;

            foreach (var file in Directory.GetFiles(currPath))
            {
                SearchResultQueue.Instance.EnQueue(file);

                if (SearchResultQueue.Instance.Count == 1)
                {
                    ThreadSynchronizer.Ready();
                }
            }

            try
            {
                foreach (var dir in Directory.GetDirectories(currPath))
                    Search(dir);
            }
            catch (Exception ex) { }
        }
    }
}
